﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmManagePlatform.UC.ComboBox
{
    public partial class Manufacturer : UserControl
    {

        List<string> _ListAll, _ListSearch;
        public Manufacturer()
        {
            InitializeComponent();
            
            cmbManufacturer.DropDownStyle = ComboBoxStyle.DropDown;
            _ListAll = new DataAccess.tb_AlarminstrumentInfo().GetInstrumentList().Select(x => x.Manufacturer).Distinct().ToList();

            //_ListAll = new List<string>()
            //{
            //    "西北测试1",
            //    "西北测试2",
            //    "西北测试3",
            //    "西北测试4",
            //    "西北测试5",
            //    "东北测试1",
            //    "东北测试2",
            //    "东北测试3",
            //    "东北测试4",
            //    "东北测试5",
            //    "西南测试1",
            //    "西南测试2",
            //    "西南测试3",
            //    "西南测试4",
            //    "西南测试5",
            //    "东南测试1",
            //    "东南测试2",
            //    "东南测试3",
            //    "东南测试4",
            //    "东南测试5",
            //};

            cmbManufacturer.Items.AddRange(_ListAll.ToArray());
            cmbManufacturer.TextUpdate += CmbManufacturer_TextUpdate;

            if (cmbManufacturer.Items.Count > 0)
            {
                cmbManufacturer.SelectedIndex = 0;
            }
        }

        private void CmbManufacturer_TextUpdate(object sender, EventArgs e)
        {
            try
            {
                if (cmbManufacturer.DropDownStyle == ComboBoxStyle.DropDownList) return;
                string stringNew = cmbManufacturer.Text;
                _ListSearch = new List<string>();

                cmbManufacturer.Items.Clear();
                for (int i = 0; i < _ListAll.Count; i++)
                {
                    if (string.IsNullOrWhiteSpace(stringNew) || _ListAll[i].Contains(stringNew))
                        _ListSearch.Add(_ListAll[i]);
                }

                if (_ListSearch.Count <= 0)
                    _ListSearch.AddRange(_ListAll);
                cmbManufacturer.Items.AddRange(_ListSearch.ToArray());

                this.Cursor = Cursors.Arrow;
                cmbManufacturer.SelectionStart = cmbManufacturer.Text.Length;
                //cmbManufacturer.DroppedDown = true;
                this.Cursor = Cursors.Arrow;
            }
            catch (Exception ex)
            {
                string s1 = string.Empty;
            }
        }

        public int SelectedIndex
        {
            get
            {
                return cmbManufacturer.SelectedIndex;
            }
            set
            {
                cmbManufacturer.SelectedIndex = value;
            }
        }
        public object SelectedItem
        {
            get
            {
                return cmbManufacturer.SelectedItem;
            }
            set
            {
                cmbManufacturer.SelectedItem = value;
            }
        }
        public string Text
        {
            get
            {
                return cmbManufacturer.Text;
            }
            set
            {
                cmbManufacturer.Text = value;
            }
        }
    }
}
